import Router from '@koa/router'
import { map, pick } from 'lodash'
import { insert, like, select, delete as sqlDelete, update } from 'mysql-bricks'
import { query } from '../models'

/**
 * 获取停车场列表
 */
export const getParkPageController: Router.Middleware = async (ctx) => {
    const picked = pick(ctx.query, ['name'])
    const whereExp = map(picked, (v, k) => like(k, `%${v}%`))
    const page = ctx.query.page || 1
    // 生成sql
    const sql = select(`p.*,
    (SELECT COUNT(*) FROM park_space ps WHERE ps.parkId = p.id AND ps.type = 0 AND ps.status = 0) AS emptyPublicSC,
    (SELECT COUNT(*) FROM park_space ps WHERE ps.parkId = p.id AND ps.type = 0 AND ps.status = 1) AS notEmptyPublicSC,
    (SELECT COUNT(*) FROM park_space ps WHERE ps.parkId = p.id AND ps.type = 1 AND ps.status = 0) AS emptyPrivateSC,
    (SELECT COUNT(*) FROM park_space ps WHERE ps.parkId = p.id AND ps.type = 1 AND ps.status = 1) AS notEmptyPrivateSC
`)
        .from('park p')
        .where(...whereExp)
        .limit(+ctx.query.limit || 10)
        .offset((+page - 1) * 10)
        .toString()
    console.log(sql)
    // 执行sql
    const res = await query(sql)
    const total = await query('select count(*) as total from park')
    // 返回数据
    ctx.body = {
        total: total[0].total,
        rows: res,
    }
}

/**
 * 获取停车场下拉框列表
 */
export const getParkOptionListController: Router.Middleware = async (ctx) => {
    // 生成sql
    const sql = select('id', 'name').from('park').toString()
    // 执行sql
    const res = (await query(sql)) as any

    // 返回数据
    ctx.body = res
}

/**
 * 获取停车场空位数量
 */
export const getParkEmptySpaceCntController: Router.Middleware = async (
    ctx
) => {
    // 执行sql
    const res = (await query(`SELECT p.name AS parkName, p.id AS parkId,
    (SELECT COUNT(*) FROM park_space ps WHERE ps.parkId = p.id AND ps.type = 0 AND ps.status = 0) AS emptyPublicCnt,
    (SELECT COUNT(*) FROM park_space ps WHERE ps.parkId = p.id AND ps.type = 0 AND ps.status = 1) AS notEmptyPublicCnt,
    (SELECT COUNT(*) FROM park_space ps WHERE ps.parkId = p.id AND ps.type = 1 AND ps.status = 0) AS emptyPrivateCnt,
    (SELECT COUNT(*) FROM park_space ps WHERE ps.parkId = p.id AND ps.type = 1 AND ps.status = 1) AS notEmptyPrivateCnt
FROM park p;`)) as any

    // 返回数据
    ctx.body = res
}

/**
 * 添加停车场
 */
export const postParkController: Router.Middleware = async (ctx) => {
    // // 获取请求参数
    const data = ctx.request.body
    // // 生成sql
    const sql = insert().into('park').values(data).toString()
    // // 执行sql
    const { insertId } = (await query(sql)) as any
    // 固定车位数量和自由车位数量
    const { maxPrivateSC, maxPublicSC } = data
    // 生成固定车位数据
    const privateDataArr = Array.from({ length: maxPrivateSC }, (_, i) => ({
        parkId: insertId,
        type: 1,
        number: (i + 1).toString().padStart(3, '0'),
    }))
    // 生成自由车位数据
    const publicDataArr = Array.from({ length: maxPublicSC }, (_, i) => ({
        parkId: insertId,
        type: 0,
        number: (i + 1 + maxPrivateSC).toString().padStart(3, '0'),
    }))
    // 生成创建车位sql
    const parkSpaceSql = insert()
        .into('park_space')
        .values([...privateDataArr, ...publicDataArr])
        .toString()
    // 执行sql
    console.log(parkSpaceSql)
    await query(parkSpaceSql)
    // 返回数据
    ctx.body = {
        id: insertId,
    }
}

/**
 * 修改停车场信息
 */
export const putParkByIdController: Router.Middleware = async (ctx) => {
    const id = +ctx.params.id!
    const data = ctx.request.body
    // 生成sql
    const sql = update('park', data).where('id', id).toString()
    // 执行sql
    const res = await query(sql)
    // 返回数据
    ctx.body = true
}

/**
 * 删除停车场
 */
export const deleteParkByIdController: Router.Middleware = async (ctx) => {
    const id = +ctx.params.id!
    // 生成sql
    const sql = sqlDelete().from('park').where('id', id).toString()
    // 执行sql
    const res = await query(sql)
    // 返回数据
    ctx.body = true
}
